#!/usr/bin/python3
import json
import sys
import re

from catalog_update.upgrade_strategy import datetime_versioning


VERSION_REG = r'2[3-9][0-1][0-9][0-3][0-9]'


def newer_mapping(image_tags):
    key = list(image_tags.keys())[0]
    tags = []
    for tag in image_tags[key]:
        match = re.fullmatch(VERSION_REG, tag)
        if match:
            tags.append(tag.split('-')[0])

    version = datetime_versioning(list(tags), '%y%m%d')
    if not version:
        return {}

    return {
        'tags': {key: f'{version}'},
        'app_version': version,
    }


if __name__ == '__main__':
    try:
        versions_json = json.loads(sys.stdin.read())
    except ValueError:
        raise ValueError('Invalid json specified')

    print(json.dumps(newer_mapping(versions_json)))
